package studentinformationmanagementsystem.dao.impl;

import studentinformationmanagementsystem.dao.BaseDao;
import studentinformationmanagementsystem.dao.CourseDao;
import studentinformationmanagementsystem.entity.Classandgrade;
import studentinformationmanagementsystem.entity.Course;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class CourseDaolmpl extends BaseDao implements CourseDao {


    /**
     * 对课程信息进行添加
     * @param course  课程对象
     * @return  受影响的记录行数
     */
    @Override
    public int insert(Course course) {
        String sql = "INSERT INTO `t_course` (`coursename`, `coursecredit`, `period`, `type`, `number`) VALUES (? , ? , ? , ? , ?)";
        return super.update(sql,course.getCoursename(),course.getCoursecredit(),course.getPeriod(),course.getType(),course.getNumber());
    }



    /**
     *  根据ID更新课程信息
     * @param course  课程对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Course course) {
        String sql = "UPDATE `t_course` SET `coursename` = ? , `coursecredit` = ? , `period` = ? , `type` = ? , `number` = ? WHERE `courseid` = ? ";
        return super.update(sql,course.getCoursename(),course.getCoursecredit(),course.getPeriod(),course.getType(),course.getNumber(),course.getCourseid());
    }


    /**
     *  根据ID删除课程
     * @param course 课程ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Course course) {
        String sql = "DELETE FROM `t_course` WHERE `courseid` = ?";
        return super.update(sql, course.getCourseid());
    }

    /**
     * 查询所有课程信息
     * @return 所有课程信息
     */
    @Override
    public List<Course> queryAll() {
        String sql = "SELECT * FROM `t_course`";
        return super.queryForList(Course.class, sql);
    }

    /**
     * 根据课程ID查询课程信息
     * @param course 课程ID
     * @return 课程信息
     */
    @Override
    public Course queryById(Course course) {
        String sql = "SELECT * FROM `t_course` WHERE `courseid` = ?";
        return super.queryForOne(Course.class, sql, course.getCourseid());
    }


    /**
     * 根据课程名称查询课程信息
     * @param course 课程名称
     * @return 课程信息
     */
    @Override
    public Course queryByCourseNname(Course course) {
        String sql = "SELECT * FROM `t_course` WHERE `coursename` = ?";
        return super.queryForOne(Course.class, sql,course.getCoursename());
    }

    /**
     * 查询总记录条数
     */
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `t_course`";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    /**
     * 分页查询DAO层方法
     *
     * @param begin    起始记录行数
     * @param pageSize 每页展示数量
     * @return
     */
    @Override
    public List<Course> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `t_course` LIMIT ?, ?";
        return super.queryForList(Course.class, sql, begin, pageSize);
    }

    @Override
    public Course queryByNumber(Course course) {
        String sql = "SELECT * FROM `t_course` WHERE `number` = ?";
        return super.queryForOne(Course.class, sql,course.getNumber());
    }

}
